/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestAction.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.awt;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestAction {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ActionFrame frame = new ActionFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ActionFrame extends JFrame {

	private static final long serialVersionUID = -400924213748822517L;

	private JPanel buttonPanel = null;

	public ActionFrame() {
		this.setTitle("Action Test");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);

		Action yellowAction = new ColorAction("Yellow", Color.YELLOW);
		Action blueAction = new ColorAction("Blue", Color.BLUE);
		Action redAction = new ColorAction("Red", Color.RED);

		// 为按钮添加动作
		buttonPanel = new JPanel();
		buttonPanel.add(new JButton(yellowAction));
		buttonPanel.add(new JButton(blueAction));
		buttonPanel.add(new JButton(redAction));

		// 关联输入键
		InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		inputMap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
		inputMap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
		inputMap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

		// 关联动作名称
		ActionMap actionMap = buttonPanel.getActionMap();
		actionMap.put("panel.yellow", yellowAction);
		actionMap.put("panel.blue", blueAction);
		actionMap.put("panel.red", redAction);

		this.getContentPane().add(buttonPanel);
	}

	/**
	 * Color Action
	 * 
	 * @author 不落的太阳(Sean Yang aka ShortPeace)
	 * @version 1.0
	 * @since jdk 1.8
	 *
	 */
	private class ColorAction extends AbstractAction {

		private static final long serialVersionUID = 4682237573127184103L;

		public ColorAction(String name, Color color) {
			this.putValue(Action.NAME, name);
			this.putValue(Action.SHORT_DESCRIPTION, "设置面板颜色为" + name);
			this.putValue("color", color);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			Color color = (Color) this.getValue("color");
			buttonPanel.setBackground(color);
		}
	}
}